package com.qczl.vehicle.controller;

import com.qczl.common.core.utils.DateUtils;
import com.qczl.common.core.utils.poi.ExcelUtil;
import com.qczl.common.core.web.controller.BaseController;
import com.qczl.common.core.web.domain.AjaxResult;
import com.qczl.common.core.web.page.TableDataInfo;
import com.qczl.common.log.annotation.Log;
import com.qczl.common.log.enums.BusinessType;
import com.qczl.common.security.utils.SecurityUtils;
import com.qczl.vehicle.domain.Brand;
import com.qczl.vehicle.service.IBrandService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 品牌管理Controller
 *
 * @author zz
 * @date 2025-09-12
 */
@Tag(name = "车辆品牌管理")
@RestController
@RequestMapping("/brand")
public class BrandController extends BaseController {
    @Autowired
    private IBrandService brandService;

    /**
     * 查询品牌管理列表
     */
    //@RequiresPermissions("vehicle:brand:list")
//    @Operation(summary = "查询车辆品牌管理列表")
//    @GetMapping("/list")
//    public TableDataInfo list(Brand brand) {
//        startPage();
//        List<Brand> list = brandService.selectBrandList(brand);
//        return getDataTable(list);
//    }
    @Operation(summary = "查询车辆品牌管理列表")
    @GetMapping("/list")
    public TableDataInfo list(Brand brand) {
        startPage();
        List<Brand> list = brandService.selectBrandList(brand);
        return getDataTable(list);
    }

    /**
     * 导出品牌管理列表
     */
    //@RequiresPermissions("vehicle:brand:export")
    @Log(title = "车辆品牌管理", businessType = BusinessType.EXPORT)
    @Operation(summary = "导出车辆品牌管理列表")
    @PostMapping("/export")
    public void export(HttpServletResponse response, Brand brand) {
        List<Brand> list = brandService.selectBrandList(brand);
        ExcelUtil<Brand> util = new ExcelUtil<Brand>(Brand.class);
        util.exportExcel(response, list, "品牌管理数据");
    }

    /**
     * 获取品牌管理详细信息
     */
    //@RequiresPermissions("vehicle:brand:query")
    @Operation(summary = "获取车辆品牌管理详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        //return success(brandService.selectBrandById(id));
        return success(brandService.getById(id));
    }

    /**
     * 新增品牌管理
     */
    //@RequiresPermissions("vehicle:brand:add")
    @Operation(summary = "新增车辆品牌")
    @Log(title = "车辆品牌管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Brand brand) {
        //return toAjax(brandService.insertBrand(brand));
        brand.setCreateTime(DateUtils.getNowDate()); //设置创建时间
        brand.setCreateBy(SecurityUtils.getUsername()); //设置创建人
        return toAjax(brandService.save(brand));
    }

    /**
     * 修改品牌管理
     */
    //@RequiresPermissions("vehicle:brand:edit")
    @Operation(summary = "修改车辆品牌")
    @Log(title = "车辆品牌管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Brand brand) {
        //return toAjax(brandService.updateBrand(brand));
        brand.setUpdateTime(DateUtils.getNowDate()); //设置更新时间
        brand.setUpdateBy(SecurityUtils.getUsername()); //设置更新人
        return toAjax(brandService.updateById(brand));

    }

    /**
     * 删除品牌管理
     */
    //@RequiresPermissions("vehicle:brand:remove")
    @Operation(summary = "删除车辆品牌")
    @Log(title = "车辆品牌管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        //return toAjax(brandService.deleteBrandByIds(ids));
        return toAjax(brandService.removeByIds(Arrays.asList(ids)));
    }
}
